<?php

/**
 * Description of HomeComtroller
 * @author : Yeu tinh
 * @since Math 11, 2013
 */
class HomeController extends AppController {

    public $name = 'Home';
    public $uses = array('Setting', 'Catproduct', 'Product');

    public function beforeFilter() {
        parent::beforeFilter();
    }

    public function index() {
        $this->set('no_backlink', 'no_backlink');

        $list_parent_id = $this->Catproduct->find('all', array(
            'fields' => array('id', 'name', 'name_eng', 'link', 'images', 'mau'),
            'conditions' => array(
                'Catproduct.status' => 1,
                'Catproduct.parent_id' => 6
            )
        ));

        //lay tin noi bat 'Product.choose2' => 1
        $listtinnoibat = array();
        foreach($list_parent_id as $values){
            $cat_id = $this->multiMenuProduct($values['Catproduct']['id'], null);
            $cat_id[$values['Catproduct']['id']] = $values['Catproduct']['id'];

            //gop ten cha + noi dung
            $listtinnoibat[] = array(
                'id' => $values['Catproduct']['id'],
                'name' => $values['Catproduct']['name'],
                'name_eng' => $values['Catproduct']['name_eng'],
                'link' => $values['Catproduct']['link'],
                'images' => $values['Catproduct']['images'],
                'mau' => $values['Catproduct']['mau'],
            );
        }
        $this->set('listtinnoibat', $listtinnoibat);

        //lay ra tin moi 'Product.choose4' => 1
        $listtinmoi = array();
        $cat_id = null;
        foreach($list_parent_id as $values){
            $cat_id = $this->multiMenuProduct($values['Catproduct']['id'], null);
            $cat_id[$values['Catproduct']['id']] = $values['Catproduct']['id'];

            //gop ten cha + noi dung
            $listtinmoi[] = array(
                'id' => $values['Catproduct']['id'],
                'name' => $values['Catproduct']['name'],
                'name_eng' => $values['Catproduct']['name_eng'],
                'link' => $values['Catproduct']['link'],
                'images' => $values['Catproduct']['images'],
                'mau' => $values['Catproduct']['mau'],
            );
        }
        $this->set('listtinmoi', $listtinmoi);
    }

    public function ajax($choose,$cat_id,$sub_cat = null){
        if(empty($sub_cat)) $sub_cat = $cat_id;

        if($choose == 1) {
            $list = $this->Product->find('all', array(
                'fields' => array('id', 'name', 'images', 'name_eng', 'link', 'shortdes', 'shortdes_eng', 'modified', 'user_id'),
                'conditions' => array(
                  'Product.status' => 1,
                  'Product.choose4' => 1,
                  'Product.cat_id' => array($sub_cat) + $this->multiMenuProduct($sub_cat)
                ),
                'order' => 'Product.order DESC, Product.id DESC',
                'limit' => '63'
            ));
            $shit = 'one';
        }

        else{
            $list = $this->Product->find('all', array(
                'fields' => array('id', 'name', 'images', 'name_eng', 'link', 'shortdes', 'shortdes_eng', 'modified', 'user_id'),
                'conditions' => array(
                  'Product.status' => 1,
                  'Product.choose2' => 1,
                  'Product.cat_id' => array($sub_cat) + $this->multiMenuProduct($sub_cat)
                ),
                'order' => 'Product.order DESC, Product.id DESC',
                'limit' => '63'
            ));
            $shit = 'two';
        }
            if(empty($list)) die;
        ?>

	    <ul class="content">
            <?php
                $one_page = 7;
                foreach($list as $key => $value){
            ?>
            <?php if($key%$one_page==0){?>
            <li>
                <div class="pull-left new-left">
                    <ul>
            <?php $key_img = $key;}?>
                        <li>
                            <a href="<?php echo DOMAIN.$value['Product']['link'];?>.html" title="<?php echo $value['Product']['name'.LANGUAGE];?>">
                                <h3><?php echo shortDesc($value['Product']['name'.LANGUAGE], 22);?></h3>
                            </a>
                        </li>
            <?php if(($key+1)%($one_page)==0 || empty($list[$key+1])){?>
                    </ul>
                    <div class="clear-main"></div>
                </div>
                <div class="pull-right new-right">
                    <div class="title-new-nb">
                        <a href="<?php echo DOMAIN.$list[$key_img]['Product']['link'];?>.html" title="<?php echo $list[$key_img]['Product']['name'.LANGUAGE];?>">
                            <?php echo shortDesc($list[$key_img]['Product']['name'.LANGUAGE], 30);?>
                        </a>
                        <div class="ext-new">
                            <a href="" title="">Minh thanh</a>
                        </div>
                    </div><!--end title-new-nb-->
                    <a href="<?php echo DOMAIN.$list[$key_img]['Product']['link'];?>.html" title="<?php echo $list[$key_img]['Product']['name'.LANGUAGE];?>">
                        <img src="<?php echo DOMAIN;?>img/h188/<?php echo $list[$key_img]['Product']['images'];?>" title="<?php echo $list[$key_img]['Product']['name'.LANGUAGE];?>" alt="<?php echo $list[$key_img]['Product']['name'.LANGUAGE];?>"/>
                    </a>
                </div>
            </li>
            <?php }?>
            <?php }?>
        </ul>
        <script>
            $(document).ready(function(){
            	$('#<?php echo 'pajinate_'.$shit.$cat_id;?>').pajinate({
                	items_per_page : 1,
                });
            });
        </script>
        <div class="page_navigation"></div>

        <?php
        die;
    }

    public function multiMenuProduct($parentid = null, $trees = array()) {
        $parmenu = $this->Catproduct->find('all', array(
            'conditions' => array(
                'Catproduct.parent_id' => $parentid,
                'Catproduct.status' => 1
            ),
            'order' => 'Catproduct.order ASC'
            ));
        if (count($parmenu) > 0) {
            foreach ($parmenu as $field) {
                $trees[$field['Catproduct']['id']] = $field['Catproduct']['id'];
                $trees = $this->multiMenuProduct($field['Catproduct']['id'], $trees);
            }
        }
        return $trees;
    }

}